package com.lv.phuotbacnam.ui.fragment;

import java.util.List;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.data.DiaDiemObject;
import com.lv.phuotbacnam.bl.queries.QueriesConstant;
import com.lv.phuotbacnam.bl.utils.IntentHelper;
import com.lv.phuotbacnam.ui.Constants;
import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class diadiemdadi extends Activity {
	private GoogleMap googleMap;
	/* Current marker */
	private Marker marker;
	private LatLng latLng;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.diadiemdadi);
		/*
		 * list dia diem
		 */

		/*
		 * show map
		 */
		latLng = new LatLng(Constants.DEFAULT_LOCATION_LAT,
				Constants.DEFAULT_LOCATION_LONG);
		
		if (googleMap == null) {
			googleMap = ((MapFragment) getFragmentManager().findFragmentById(
					R.id.map)).getMap();
			// check if map is created successfully or not
			if (googleMap == null) {
				Toast.makeText(this, "Sorry! unable to create maps",
						Toast.LENGTH_SHORT).show();
			} else {
				// ParseGeoPoint p = new
				// ParseGeoPoint(Constants.DEFAULT_LOCATION_LAT,
				// Constants.DEFAULT_LOCATION_LONG);

				// showLocationIcon(p,"test");

				ParseUser currentUser = ParseUser.getCurrentUser();
				ParseQuery<ParseObject> query = ParseQuery
						.getQuery(QueriesConstant.TableDiaDiem.Table_Name);
				query.whereEqualTo(
						QueriesConstant.TableDiaDiem.COLUMN_nguoi_tao,
						currentUser);
				query.findInBackground(new FindCallback<ParseObject>() {

					@Override
					public void done(List<ParseObject> diadiems,
							ParseException e) {
						if (e == null && diadiems.size() > 0) {
							for (final ParseObject diadiem : diadiems) {
								showLocationIcon(
										diadiem.getParseGeoPoint(QueriesConstant.TableDiaDiem.COLUMN_vi_tri),
										diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem));
								googleMap.setOnMarkerClickListener(new OnMarkerClickListener()
						        {

						            @Override
						            public boolean onMarkerClick(Marker arg0) {
						            	Intent intent = new Intent(diadiemdadi.this, ChitietDiadiem2.class);
										 IntentHelper.addObjectForKey(diadiem,"diadiem");
										 startActivity(intent);
						                return true;
						            }

						        });      
							}
						}

					}
				});
			}
			

			CameraUpdate center = CameraUpdateFactory.newLatLng(latLng);
			CameraUpdate zoom = CameraUpdateFactory
					.zoomTo(Constants.CAMERA_ZOOM_DEFAULT);

			googleMap.moveCamera(center);
			googleMap.animateCamera(zoom);
		}

	}

	private void showLocationIcon(ParseGeoPoint parseGeoPoint, String title) {

		// create marker
		MarkerOptions markerOptions = new MarkerOptions().position(
				new LatLng(parseGeoPoint.getLatitude(), parseGeoPoint
						.getLongitude())).title(title);// .title("Hello Maps ");
		
		// Set icon
		// markerOptions.icon(BitmapDescriptorFactory
		// .fromResource(R.drawable.maker));

		googleMap.addMarker(markerOptions);
		 
	}

}
